package com.example.springboot.service;

import com.example.springboot.Mapper.EbookMapper;
import com.example.springboot.pojo.Ebook;
import com.example.springboot.pojo.EbookExample;
import com.example.springboot.request.EbookQueryRequest;
import com.example.springboot.request.EbookSaveRequest;
import com.example.springboot.response.EbookQueryResponse;
import com.example.springboot.response.PageResponse;
import com.example.springboot.util.CopyUtil;
import com.example.springboot.util.SnowFlake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ServiceEbook {

    private static final Logger LOG= LoggerFactory.getLogger(ServiceEbook.class);

    @Resource
    private EbookMapper ebookMapper;

    @Resource
    private SnowFlake snowFlake;

    public PageResponse<EbookQueryResponse> list(EbookQueryRequest ebookQueryRequest){



        EbookExample ebookExample = new EbookExample();
        EbookExample.Criteria criteria = ebookExample.createCriteria();


        if(!ObjectUtils.isEmpty(ebookQueryRequest.getName())){
            criteria= criteria.andNameLike("%"+ ebookQueryRequest.getName()+"%");
        }
        if(!ObjectUtils.isEmpty(ebookQueryRequest.getCategory2Id())){
            criteria= criteria.andCategory2IdEqualTo( ebookQueryRequest.getCategory2Id());
        }
        PageHelper.startPage(ebookQueryRequest.getPage(), ebookQueryRequest.getSize());
        List<Ebook> ebookList=ebookMapper.selectByExample(ebookExample);


        PageInfo<Ebook> pageInfo = new PageInfo<>(ebookList);
        LOG.info("总行数："+pageInfo.getTotal());
        LOG.info("总页数：" + +pageInfo.getPages());



        /*List<EbookResponse> responseList=new ArrayList<>();
        for( Ebook ebook:ebookList){
            *//*EbookResponse ebookResponse=new EbookResponse();
            BeanUtils.copyProperties(ebook,ebookResponse);*//*

            //使用CopyUtil代替上面的那两句
        EbookResponse ebookResponse = CopyUtil.copy(ebook,EbookResponse.class);


            responseList.add(ebookResponse);

        }*/


        //列表复制
        List<EbookQueryResponse> list= CopyUtil.copyList(ebookList, EbookQueryResponse.class);

        PageResponse<EbookQueryResponse> pageResponse = new PageResponse<>();
        pageResponse.setTotal(pageInfo.getTotal());
        pageResponse.setList(list);
        return pageResponse;
    }

    /*
    * 保存
    * */
    public void save(EbookSaveRequest request){
        Ebook ebook=CopyUtil.copy(request,Ebook.class);
        if(ObjectUtils.isEmpty(request.getId())){
            //新增
            ebook.setId(snowFlake.nextId());
            ebookMapper.insert(ebook);
        }
        else{
            //更新
            ebookMapper.updateByPrimaryKey(ebook);
        }
    }

    /*
    * 删除
    * */
    public void delete(Long id){
        ebookMapper.deleteByPrimaryKey(id);

    }
}
